Security News
Highlights from the 2024 Rails Community Survey
A record 2,709 developers participated in the 2024 Ruby on Rails Community Survey, revealing key tools, practices, and trends shaping the Rails ecosystem.
import-from
Advanced tools
The npm package 'import-from' allows you to import modules from a specific path, bypassing the default behavior of Node.js module resolution. This can be particularly useful in scenarios where you need to dynamically choose different versions of a module or when working with plugins that may not be in the node_modules directory of the current project.
Import module from a specific directory
This feature allows you to import a module by specifying the directory from which the module should be loaded. This is useful for loading modules dynamically based on runtime conditions or configurations.
const importFrom = require('import-from');
const specificModule = importFrom('/path/to/specific/directory', 'module-name');
Silent import attempt
This feature provides a way to attempt to import a module silently without throwing errors if the module does not exist. It is useful for optional dependencies that may or may not be present in a given environment.
const importFrom = require('import-from');
const tryImport = importFrom.silent('/path/to/directory', 'optional-module');
This package allows you to require modules from a string of code. It is similar to 'import-from' in that it modifies the standard module loading behavior, but it focuses on executing and importing code from strings rather than from specific file paths.
Proxyquire is designed to help with mocking dependencies for testing purposes. It allows you to replace modules in require calls within the module under test. While it also changes how modules are loaded, it is specifically tailored for testing rather than general module loading from different locations.
Import a module like with
require()
but from a given path
$ npm install --save import-from
const importFrom = require('import-from');
// There is a file at `./foo/bar.js`
importFrom('foo', './bar');
Like require()
, throws when the module can't be found.
Returns null
instead of throwing when the module can't be found.
Type: string
Directory to import from.
Type: string
What you would use in require()
.
Create a partial using a bound function if you want to import from the same fromDir
multiple times:
const importFromFoo = importFrom.bind(null, 'foo');
importFromFoo('./bar');
importFromFoo('./baz');
MIT © Sindre Sorhus
FAQs
Import a module like with `require()` but from a given path
The npm package import-from receives a total of 4,555,656 weekly downloads. As such, import-from popularity was classified as popular.
We found that import-from demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A record 2,709 developers participated in the 2024 Ruby on Rails Community Survey, revealing key tools, practices, and trends shaping the Rails ecosystem.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.